<?php namespace system\model;
//	框架的核心模型类
	use hdphp\model\Model;
//	商品表模型类
	class Goods extends Model{
//		自动检测
		 protected $validate = [
        ['gname','required','商品名不能为空',3,3]
    ];
    	protected $auto=[
//        更新时对 addtime 字段执行strtotime函数
        ['time','time','function',3,1],
    ];
//		添加方法
		public function store($var){
			if(!$this->create()) return false;
			$this->data['aid']=$_SESSION['id'];
//			p($this->data);die;
			if($var==1){
//				echo 1;die;
				$gid=$this->save();
			}else{
//				echo 2;die;
				$gid=$this->add();
			}
//			p($_POST);die;
//			写入分类数据
			$attrModel=new \system\model\goods_attr;
			$listModel=new \system\model\goods_list;
			$attr=Q('post.attr',array());
			$spec=Q('post.spec',array());
			if($var==1){
				$gid=$this->data['gid'];
				$attrModel->where('gid',$gid)->delete();
				$listModel->where('gid',$gid)->delete();
			}
//			p($attr);die;
			foreach ($attr as $k => $v) {
				if($v){
					$attrData=array(
						'gtvalue'=>$v,
						'taid'=>$k,
						'gid'=>$gid,
					);
//					p($attrData);
					$re=$attrModel->add($attrData);
				}
				
			}
//			die;
			foreach ($spec as $k => $v) {
				foreach ($v['gtvalue'] as $kk => $vv) {
					$attr=array(
						'gtvalue'=>$vv,
						'taid'=>$k,
						'gid'=>$gid,
						'added'=>$v['added'][$kk]
					);
					$attrModel->add($attr);
//					p($attr);
				}
			}
//			die;
			if($val==1){
//			删除已存在图片
				$oldData=Db::table('detail')->where('gid',$gid)->first();
				$oldData=$oldData['small'].'|'.$oldData['medium'].'|'.$oldData['big'].'|'.$oldData['photo'];
				$oldData=explode('|',$oldData);
				foreach($oldData as $k=>$v){
					if(is_file($v)){
						unlink($v);
					}
				}				
			}
			
//			小图
				foreach($this->data['photo'] as $k=>$v){
					$xv=str_replace('.', '_58X58.', $v);
//				p($v);
//				p($xv);
					$re=Image::thumb($v,$xv,58,58,6);
					$small[]=$re;
				}
				$small=implode('|', $small);
//			中图
				foreach($this->data['photo'] as $k=>$v){
					$xv=str_replace('.', '_480X480.', $v);
//				p($xv);
					$re=Image::thumb($v,$xv,480,480,6);
					$medium[]=$re;
				}
				$medium=implode('|', $medium);			
//			大图
				foreach($this->data['photo'] as $k=>$v){
					$xv=str_replace('.', '_900X900.', $v);
//				p($xv);
					$re=Image::thumb($v,$xv,900,900,6);
					$big[]=$re;
				}
				$big=implode('|', $big);
			
//			die;
//			写入表
			$detail=new \system\model\detail;
			$photos=implode('|',$this->data['photo']);
			if($var==1){
				$thumbData=array(
					'small'=>$small,
					'medium'=>$medium,
					'big'=>$big,
					'gid'=>$gid,
					'photo'=>$photos
				);
//				p($thumbData);die;				
				$detail->where('gid',$gid)->update($thumbData);
			}else{
				$thumbData=array(
					'small'=>$small,
					'medium'=>$medium,
					'big'=>$big,
					'intro'=>$this->data['intro'],
					'service'=>$this->data['service'],
					'gid'=>$gid,
					'photo'=>$photos
					);
//					p($thumbData);die;
				$detail->add($thumbData);
			}
//			die;
			return ture;
		}
	}
	
	
	
	
	
	
	
	
	?>